package com.banmao.algorithm.No201To300.No225;

import java.util.ArrayDeque;
import java.util.Queue;

public class MyStack {

    private Queue<Integer> queue1 = new ArrayDeque<>();
    private Queue<Integer> queue2 = new ArrayDeque<>();

    private Queue<Integer> currectQueue;
    private Queue<Integer> otherQueue;

    public MyStack() {
        currectQueue = queue1;
        otherQueue = queue2;
    }

    public void push(int x) {
        currectQueue.add(x);
    }

    public int pop() {
        // 将currectQueue中的元素逐一推入另一个Queue，再将currectQueue指向另一个Queue
        while(currectQueue.size() > 1) {
            otherQueue.add(currectQueue.poll());
        }
        int result = 0;
        if(currectQueue.size() > 0) {
            result = currectQueue.poll();
        }

        Queue<Integer> tempQ = currectQueue;
        currectQueue = otherQueue;
        otherQueue = tempQ;

        return result;
    }

    public int top() {
        // 将currectQueue中的元素逐一推入另一个Queue，再将currectQueue指向另一个Queue
        while(currectQueue.size() > 1) {
            otherQueue.add(currectQueue.poll());
        }
        int result = 0;
        if(currectQueue.size() > 0) {
            result = currectQueue.poll();
            otherQueue.add(result);
        }

        Queue<Integer> tempQ = currectQueue;
        currectQueue = otherQueue;
        otherQueue = tempQ;

        return result;

    }

    public boolean empty() {
        return currectQueue.isEmpty();
    }
}
